//
//  UserBlackListController.m
//  CheekiOS
//
//  Created by mac on 2017/6/23.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "UserBlackListController.h"
#import "UserBlackListCell.h"
@interface UserBlackListController ()<UITableViewDelegate, UITableViewDataSource,UserBlackListCellDelegate>

@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) UITableView *tableView;



@end

@implementation UserBlackListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavgationBarHiddenNo];
    self.title = @"黑名单列表";
    _page = 1;
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-49) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    
    [self requestData];
}

- (void)requestData{
    self.dataSource = [NSMutableArray array];
    WEAKSELF
    [HQHttpManager myBlackListWithUid:[HQHttpManager getUserId] page:@(_page) successBlock:^(BOOL isSuccess, NSDictionary *resultDic) {
        if (isSuccess) {
            if (_page == 1) {
                [self.dataSource removeAllObjects];
            }
            NSArray *array = resultDic[@"res"];
            [self.dataSource addObjectsFromArray:array];
            if (array.count == 0) {
                _page--;
            }
            [weakSelf.tableView reloadData];
        } else {
            _page--;
        }
    }];

    
}

-(void)BtnDelegateClick:(UIButton *)sender{

    NSDictionary *model = _dataSource[sender.tag];
    
    WEAKSELF
    [HQHttpManager reBlackFriendWithUid:[HQHttpManager getUserId] wid:model[@"wid"] successBlock:^(BOOL isSuccess, NSDictionary *resultDic) {
        if (isSuccess) {
            
            _page = 1;
            [SVProgressHUD showWithStatus:@"移除成功"];
            [_dataSource removeObjectAtIndex:sender.tag];
            [weakSelf.tableView reloadData];

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [SVProgressHUD dismiss];
                
            });
        }
    }];
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UserBlackListCell *cell = [UserBlackListCell cellWithTableView:tableView indexPath:indexPath];
    cell.delegate = self;
    cell.model = _dataSource[indexPath.row];
 
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 74;
}


@end
